package com.juyu.miliao.scheduler.message;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.common.constant.HostRecentDevelopmentsMqKey;
import com.juyu.miliao.common.constant.HostRecentDevelopmentsRedisKey;
import com.juyu.miliao.common.domain.dto.HostRecentDevelopmentsDTO;
import com.juyu.miliao.common.domain.vo.*;
import com.juyu.miliao.scheduler.dao.mysql.FindDynamicDao;
import com.juyu.miliao.scheduler.domin.FindDynamicInfo;
import com.juyu.miliao.scheduler.domin.FindDynamicSourceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 主播最近动态
 *
 * @author xuanqicheng
 */
@Component
@Slf4j
public class HostRecentDevelopmentsMessage {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private FindDynamicDao findDynamicDao;


    @RabbitListener(bindings = @QueueBinding(value = @Queue(HostRecentDevelopmentsMqKey.DevelopmentsInfo.QUEUE_KEY),
            exchange = @Exchange(HostRecentDevelopmentsMqKey.EXCHANGE),
            key = HostRecentDevelopmentsMqKey.DevelopmentsInfo.ROUTING_KEY))
    public void getMessage(String message) {
        if (!StringUtils.isEmpty(message)) {
            try {
                HostRecentDevelopmentsDTO dto = objectMapper.readValue(message, HostRecentDevelopmentsDTO.class);
                if (dto.getType() == 1) {
                    setFindDynamic(dto);
                } else if (dto.getType() == 2) {
                    setPhoto(dto);
                } else if (dto.getType() == 3) {
                    setVideo(dto);
                }
            } catch (Exception ex) {
                log.error("主播最近动态数据错误：" + ex.getMessage());
            }
        }
    }

    private void setFindDynamic(HostRecentDevelopmentsDTO dto) throws JsonProcessingException {
        FindDynamicInfo findDynamicInfo = findDynamicDao.getFindDynamicByFindId(dto.getFindId());
        List<FindDynamicSourceInfo> list = findDynamicDao.selectFindDynamicSourceInfoByFindId(dto.getFindId());

        HostRecentDevelopmentsFindDynamicVO vo = new HostRecentDevelopmentsFindDynamicVO();
        vo.setType(dto.getType());
        vo.setSourceType(findDynamicInfo.getSourceType());
        vo.setFindId(dto.getFindId());
        String title = findDynamicInfo.getFindTitle();
        vo.setTitle(title.length() > 5 ? title.substring(0, 5) : title);
        HostRecentDevelopmentsFindDynamicSourceInfoVO source = new HostRecentDevelopmentsFindDynamicSourceInfoVO();
        BeanUtils.copyProperties(list.get(0), source);
        vo.setSource(source);

        String value = objectMapper.writeValueAsString(vo);
        setDevelopmentsList(dto,value);
    }

    private void setPhoto(HostRecentDevelopmentsDTO dto) throws JsonProcessingException {
        HostRecentDevelopmentsPhotoVO vo = new HostRecentDevelopmentsPhotoVO();
        vo.setType(dto.getType());
        vo.setTitle("相册");
        vo.setPhotoUrl(dto.getPhotoUrl());
        String value = objectMapper.writeValueAsString(vo);
        setDevelopmentsList(dto, value);
    }


    private void setVideo(HostRecentDevelopmentsDTO dto) throws JsonProcessingException {
        HostRecentDevelopmentsVideoVO vo = new HostRecentDevelopmentsVideoVO();
        vo.setType(dto.getType());
        vo.setTitle("视频");
        vo.setVideoImageUrl(dto.getVideoImageUrl());
        vo.setVideoUrl(dto.getVideoUrl());
        String value = objectMapper.writeValueAsString(vo);
        setDevelopmentsList(dto, value);
    }

    /**
     * 设置动态列表
     *
     * @param dto
     * @param vo
     * @throws JsonProcessingException
     */
    private void setDevelopmentsList(HostRecentDevelopmentsDTO dto, String value) {
        String key = HostRecentDevelopmentsRedisKey.getHostRecentDevelopmentsKey(dto.getUserId());
        stringRedisTemplate.opsForList().leftPush(key, value);
        stringRedisTemplate.opsForList().trim(key, 0, 2);
    }

}
